<template>
	<om-addpage :menuName="menuName" :busCode="busCode" @formSubmit="formSubmit" :showHistoryBtn="false" saveBtnText="确认" :customAddFunc="true">
		<om-sub title="审核打分" subTitle="请拖动滑块打分">
			<uni-list v-for="(item,index) in itemList" :key="index">
				<uni-list-item  :showArrow="false" :showHover="false">
					<view><om-label :label="item.itemName + '（分值：'+item.standardScore+'）'+item.title" ></om-label></view>
					<view>
						<slider :max="item.standardScore" block-color="#8A6DE9" block-size="26"  :value="item.score" @change="sliderChange" show-value :data-index="index" step="1" />
					</view>
				</uni-list-item>
			</uni-list>
		</om-sub>
		<om-sub title="意见说明">
			<uni-list>
				<uni-list-item  :showArrow="false" :showHover="false">
					<view>
						<textarea 
						:maxlength="200"  
						v-model="opinion"
						auto-height 
						placeholder-style="font-size:15px;line-height:20px"
						style="margin-left: 10px;"
						placeholder="最多输入200个字"
						>
						</textarea>
					</view>
				</uni-list-item>
			</uni-list>
		</om-sub> 
		</uni-list>
	</om-addpage>
</template>

<script>
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			formdata: {
			},
			sliderData:[],
			datas:[],
			itemIdList:[],
			menuName: '',
			formId:"",
			busCode: '',
			itemList:"",
			scoreLeave:[],
			opinion:"",
			selfScoreAuditFlag:""
		};
	},
	computed: {
		...mapState(useLoginUserStore,['userId','orgId'])
	},
	methods: {
		getAuditedAuth(){
			let _that = this
			_that.om.request(
				"subjectiveEvaluate/getAuditedAuth",
				{
					formId:_that.formId
				}
			).then(res=>{
				 if(res.value.selfScoreAuditFlag){
					_that.selfScoreAuditFlag = res.value.selfScoreAuditFlag
				 }else{
					 _that.selfScoreAuditFlag = false
				 }
			})
		},
		getItemScoreList(){
			let _that = this
			_that.om.request(
				"subjectiveEvaluate/itemScoreListByH5",
				{
					formId:_that.formId
				}
			).then(res=>{
				let arr = []
				for(let index in res.value){
					let itemArr = []
					if(res.value[index]){
						for(let item of res.value[index]){
							let obj = {
								text:item.text,
								maxScore:item.children[0],
								minScore:item.children[item.children.length-1]
							}
							itemArr.push(obj)
						}
					}
					arr.push(itemArr)
				}
				_that.scoreLeave = arr
				for(let i in _that.itemList){
					_that.itemList[i].title = _that.scoreLeave[i][_that.scoreLeave[i].length - 1].text
				}
			})
		},
		/**
		 * 打分
		 */
		sliderChange(e) {
			var index = e.currentTarget.dataset.index
			var score = e.detail.value
			this.itemList[index].score = score
			for(let i in this.scoreLeave[index]){
				if(Number(this.scoreLeave[index][i].minScore.text)<= Number(score) && 
					Number(score) <= Number(this.scoreLeave[index][i].maxScore.text)){
					this.itemList[index].title = this.scoreLeave[index][i].text
				}
			}
			
		},
		formSubmit: function(formdata) {
			let _that = this
			let dataUserStr = []
			for(let item of _that.itemList){
				let iObj = {
					itemId:item.itemId,
					itemName:item.itemName,
					evaluateUserId:_that.userId,
					evaluateScore:item.score?item.score:0,
				}
				dataUserStr.push(iObj)
			}
			let obj = {
				formId:_that.formId,
				isApprove: 1,
				opinion: _that.opinion,
				businessFormCode:_that.busCode,
				fileOrgId:_that.orgId,
				PAGE_CODE:_that.busCode,
				dataUserStr:JSON.stringify(dataUserStr),
				selfScoreAuditFlag: _that.selfScoreAuditFlag
			}
			_that.om.request(
				"subjectiveEvaluate/doAudited",
				obj,
				"post"
			).then(res=>{
				if(res.succeed){
					_that.om.getFormInfo(_that.formId, _that.busCode, true)
				}else{
					_that.om.toast(res.msg)
				}
			})	
		},
	},
	onLoad: function(options) {
		let _that = this;
		_that.menuName = options.title || '审核打分';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		if(options.code){
			_that.busCode = options.code
		}else{
			_that.om.toast("没有表单编码")
		}
		if(options.fileId){
			_that.formId = options.fileId
		}else{
			_that.om.toast("没有表单ID")
		}
		_that.om.request("subjectiveEvaluate/getValue",{
			fileId:options.fileId
		}).then(res=>{
			if(res.succeed){
				let arr = [] 
				for(let item of res.value.itemList){
					let obj = {
						itemId:item.itemId,
						itemName:item.itemName,
						standardScore:item.standardScore,
						title:'',
						score:''
					}
					for(let user of item.userList){
						if(_that.userId == user.evaluateUserId){
							obj.score = user.evaluateScore
						}
					}
					arr.push(obj)
				}
				_that.itemList = arr
				_that.getItemScoreList()
				_that.getAuditedAuth()
			}else{
				_that.om.toast(res.msg)
			}
		})
		
	}	
};
</script>

<style></style>
